package com.atguigu.service.impl;

import com.atguigu.dao.UserMapper;
import com.atguigu.pojo.User;
import com.atguigu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper ;
    @Autowired
    RedisTemplate redisTemplate ;
    @Override
    public List<User> findAll() {
        //从缓存中查数据
        List<User> userList = (List<User>) redisTemplate.boundValueOps("userList").get();
        if (userList == null) {
            //从数据库读取数据
            userList = userMapper.selectAll();
            redisTemplate.boundValueOps("userList").set(userList);
            System.out.println("从数据库查找...");
        } else {
            System.out.println("从缓存中查找...");
        }
        return userList ;

    }
}
